package krautz.fitness;

import java.io.ByteArrayOutputStream;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.util.Log;

public class WeightDatabase {
	
	//if bitmapdata is the byte array then getting bitmap goes like this
	//Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length);

    public static final String KEY_ROWID    = "_id";
    public static final String KEY_DATETIME = "datetime";
    public static final String KEY_WEIGHT	= "weight";
    public static final String KEY_BODYFAT	= "bodyfat";
    public static final String KEY_WATER	= "water";
    public static final String KEY_VISCERAL = "visceral";
    public static final String KEY_MUSCLE	= "muscle";
    public static final String KEY_PICTURE	= "picture";
    public static final String KEY_COMMENTS	= "comments";

    private static final String TAG = "NotesDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    
    private static final String DATABASE_NAME = "data";
    private static final String DATABASE_TABLE = "weights";
    private static final int DATABASE_VERSION = 1;
    
    /**
     * Database creation sql statement
     */
    private static final String DATABASE_CREATE =
        "create table "+DATABASE_TABLE+ " ("+KEY_ROWID+" integer primary key autoincrement, "
        + KEY_DATETIME+" integer not null, "
        + KEY_WEIGHT+" real not null, "
        + KEY_BODYFAT+" real, "
        + KEY_WATER+" real, "
        + KEY_VISCERAL+" real, "
        + KEY_MUSCLE+" real, "
        + KEY_PICTURE+" blob, "
        + KEY_COMMENTS+" text);";

    private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);
            //TODO Need to copy over the rows from the database
            onCreate(db);
        }
    }
    
    public WeightDatabase(Context ctx) {
        this.mCtx = ctx;
    }
    
    public void open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
    }

    public void close() {
        mDbHelper.close();
    }
	
    public long createWeightRecord(WeightRecord record) {
        ContentValues row = new ContentValues();
        
        row.put(KEY_DATETIME, record.get_datetime());
        row.put(KEY_WEIGHT,   record.get_weight());
        
        Double fat = record.get_bodyFatPercent();
        if(fat == null){
        	row.putNull(KEY_BODYFAT);
        }
        else{
        	row.put(KEY_BODYFAT,  fat);
        }
        
        Double water = record.get_waterPercent();
        if(water == null){
        	row.putNull(KEY_BODYFAT);
        }
        else{
        	row.put(KEY_WATER,  water);
        }
        
        Double visceral = record.get_visceralFat();
        if(visceral == null){
        	row.putNull(KEY_VISCERAL);
        }
        else{
        	row.put(KEY_VISCERAL,  visceral);
        }
        
        Bitmap picture = record.get_picture();
        if(picture == null){
        	row.putNull(KEY_PICTURE);
        }
        else{
        	ByteArrayOutputStream stream = new ByteArrayOutputStream();
        	picture.compress(CompressFormat.JPEG, 100, stream);
        	row.put(KEY_PICTURE, stream.toByteArray());
        }
        
        Double muscle = record.get_muscleMass();
        if(muscle == null){
        	row.putNull(KEY_MUSCLE);
        }
        else{
        	row.put(KEY_MUSCLE,  muscle);
        }
        
        String comments = record.get_comments();
        if(comments == null){
        	row.putNull(KEY_COMMENTS);
        }
        else{
        	row.put(KEY_COMMENTS,  comments);
        }
        
        return mDb.insert(DATABASE_TABLE, null, row);
    }
    
    
    
}
